import concurrent.futures
import time
import random

# 定义计算密集型任务
def compute_task(n):
    total = 0
    for i in range(n):
        total += i * random.random()
    return total

# 测试单线程性能
start_time = time.time()
compute_task(10000000)
end_time = time.time()
print(f"单线程任务运行时间: {end_time - start_time} 秒")

# 测试多线程性能
start_time = time.time()
with concurrent.futures.ThreadPoolExecutor() as executor:
    futures = [executor.submit(compute_task, 10000000) for _ in range(8)]
    results = [f.result() for f in futures]
end_time = time.time()
print(f"多线程任务运行时间: {end_time - start_time} 秒")
